﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using System.IO;

public partial class controls_admin_MenuAdd : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            getSec_Admin_Modules();
        }
    }
    protected void getSec_Admin_Modules()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, 0);
            ddlParentMenu.Items.Add(new ListItem("Chọn Menu", "0"));
            if (objDT.Rows.Count > 0)
            {
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentMenu.Items.Add(new ListItem(objDT.Rows[i]["DESCRIPTION"].ToString(), objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()));
                    getChildSec_Admin_Modules(Convert.ToInt32(objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()), "");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getChildSec_Admin_Modules(int _parent_id, string ident)
    {
        try
        {
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, _parent_id);
            if (objDT.Rows.Count > 0)
            {
                ident += "--- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentMenu.Items.Add(new ListItem(ident + objDT.Rows[i]["DESCRIPTION"].ToString(), objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()));
                    getChildSec_Admin_Modules(Convert.ToInt32(objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtDesc.Text = "";
        txtMenuName.Text = "";
        cboDisOnHome.Checked = true;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = (DataTable)Session["admin"];
            string filename = string.Empty;
            try
            {
                if (fulTHUMB_ICON.HasFile)
                {
                    filename = fulTHUMB_ICON.FileName;

                    int count = 0;
                    while (File.Exists(Server.MapPath("~/resources/images/icons/") + filename))
                    {
                        filename = count + "_" + fulTHUMB_ICON.FileName;
                        count++;
                    }
                    fulTHUMB_ICON.SaveAs(Server.MapPath("~/resources/images/icons/") + filename);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file! " + ex.Message);
                return;
            }
            string strResult = BO_Sec_Admin_Modules.Insert(Convert.ToInt32(ddlParentMenu.SelectedValue),
                txtMenuName.Text,
                txtDesc.Text, filename,
                cboDisOnHome.Checked,
                Convert.ToInt32(txtSortDisplay.Text), 1, DateTime.Now,
                DateTime.Now,
                objDT.Rows[0]["USER_NAME"].ToString(),
                DateTime.Now,
                "",
                true);
            if (strResult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Hệ Thống", "Thêm mới menu hệ thống thành công!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Có lỗi trong quá trình xử lý, thử lại sau!");
            btnAdd_Click(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(12, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_PARENT":
                    NOTE_PARENT.InnerText = value;
                    break;
                case "NOTE_NAME":
                    NOTE_NAME.InnerText = value;
                    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_SORT":
                    NOTE_SORT.InnerText = value;
                    break;
                case "NOTE_ICON":
                    NOTE_ICON.InnerText = value;
                    break;
                case "NOTE_DISPLAY":
                    NOTE_DISPLAY.InnerText = value;
                    break;
            }
        }
    }
}
